//
//  TSystemMessageCell.m
//  UIKit
//
//  Created by kennethmiao on 2018/9/17.
//  Copyright © 2018年 kennethmiao. All rights reserved.
//

#import "TSystemMessageCell.h"
#import "THeader.h"

@implementation TSystemMessageCellData
@end

@interface TSystemMessageCell ()
{
    TSystemMessageCellData *customDataModel;
}
@end

@implementation TSystemMessageCell

- (CGFloat)getHeight:(TSystemMessageCellData *)data {
    if (data.customModel.type&&data.customModel.type.length) {
        if ([data.customModel.type isEqualToString:@"tip"]) {
            _content.text = data.customModel.content;
            CGSize size = [_content sizeThatFits:CGSizeMake(TSystemMessageCell_Text_Width_Max, MAXFLOAT)];
            size.height += 2 * TSystemMessageCell_Margin + 2 * TMessageCell_Margin;
            return size.height;
        } else if ([data.customModel.type isEqualToString:@"namecard_exchange"]) {
            CGFloat height = 100*Scale + 2 * TSystemMessageCell_Margin + 2 * TMessageCell_Margin;
            return height;
        } else if ([data.customModel.type isEqualToString:@"resume_apply"]) {
            CGFloat height = 100*Scale + 2 * TSystemMessageCell_Margin + 2 * TMessageCell_Margin;
            return height;
        } else if ([data.customModel.type isEqualToString:@"apply_video"]) {
            CGFloat height = 100*Scale + 2 * TSystemMessageCell_Margin + 2 * TMessageCell_Margin;
            return height;
        } else if ([data.customModel.type isEqualToString:@"apply_voice"]) {
            CGFloat height = 100*Scale + 2 * TSystemMessageCell_Margin + 2 * TMessageCell_Margin;
            return height;
        } else if ([data.customModel.type isEqualToString:@"namecard"]) {
//            260x180
            CGFloat height = (WIDTH-40.0*Scale)*685/1005 + 2 * TSystemMessageCell_Margin + 2 * TMessageCell_Margin;
            return height;
        } else if ([data.customModel.type isEqualToString:@"resume"]) {
            CGFloat height = 100*Scale + 2 * TSystemMessageCell_Margin + 2 * TMessageCell_Margin;
            return height;
        } else if ([data.customModel.type isEqualToString:@"notice"]) {
            [self.noticeBgView reloadDataForModel:data.customModel];
            CGFloat height = self.noticeBgView.viewHeight + 2 * TSystemMessageCell_Margin + 2 * TMessageCell_Margin;
            return height;
        } else if ([data.customModel.type isEqualToString:@"job_info"]) {
            CGFloat height = 100*Scale + 2 * TSystemMessageCell_Margin + 2 * TMessageCell_Margin;
            return height;
        } else if ([data.customModel.type isEqualToString:@"resume_info"]) {
            CGFloat height = 100*Scale + 2 * TSystemMessageCell_Margin + 2 * TMessageCell_Margin;
            return height;
        } else if ([data.customModel.type isEqualToString:@"location"]) {
            CGFloat height = 90*Scale + 2 * TSystemMessageCell_Margin + 2 * TMessageCell_Margin;
            return height;
        } else {
            return 0.0;
        }
    } else {
        _content.text = data.content;
        CGSize size = [_content sizeThatFits:CGSizeMake(TSystemMessageCell_Text_Width_Max, MAXFLOAT)];
        size.height += 2 * TSystemMessageCell_Margin + 2 * TMessageCell_Margin;
        return size.height;
    }
}
- (void)setData:(TSystemMessageCellData *)data {
    customDataModel = data;
    //set data
    self.bgView.hidden = YES;
    self.cardview.hidden = YES;
    self.personalView.hidden = YES;
    self.jobView.hidden = YES;
    self.locationBgView.hidden = YES;
    self.noticeBgView.hidden = YES;
    if (data.customModel.type&&data.customModel.type.length) {
        if ([data.customModel.type isEqualToString:@"tip"]) {
            _content.text = data.customModel.content;
            if ([data.customModel.style isEqualToString:@"normal"]) {
                _content.textColor = COLOR_OF_WHITE;
            } else if ([data.customModel.style isEqualToString:@"fail"]) {
                _content.textColor = THEMECOLOR_OF_RED;
            }
            _content.backgroundColor = THEMECOLOR_OF_TEXT_cccccc;
            CGSize textSize = [_content sizeThatFits:CGSizeMake(TSystemMessageCell_Text_Width_Max, MAXFLOAT)];
            CGFloat width = textSize.width + 2 * TSystemMessageCell_Margin;
            CGFloat height = textSize.height + 2 * TSystemMessageCell_Margin;
            _content.frame = CGRectMake((Screen_Width - width) * 0.5, TMessageCell_Padding, width, height);
        } else if ([data.customModel.type isEqualToString:@"namecard_exchange"]) {
            self.bgView.hidden = NO;
            self.titleLbl.text = data.customModel.content;
            self.okBtn.frame = CGRectMake(EndX(self.noBtn), 65*Scale, ViewWidth(self.bgView)*0.5, 35*Scale);
            [self.okBtn setTitleColor:THEMECOLOR_OF_RED forState:UIControlStateNormal];
            [self.okBtn setTitle:@"同意" forState:UIControlStateNormal];
        } else if ([data.customModel.type isEqualToString:@"resume_apply"]) {
            self.bgView.hidden = NO;
            self.titleLbl.text = data.customModel.content;
            self.okBtn.frame = CGRectMake(EndX(self.noBtn), 65*Scale, ViewWidth(self.bgView)*0.5, 35*Scale);
            [self.okBtn setTitleColor:THEMECOLOR_OF_RED forState:UIControlStateNormal];
            [self.okBtn setTitle:@"同意" forState:UIControlStateNormal];
        } else if ([data.customModel.type isEqualToString:@"apply_video"]) {
            self.bgView.hidden = NO;
            self.titleLbl.text = data.customModel.content;
            self.okBtn.frame = CGRectMake(EndX(self.noBtn), 65*Scale, ViewWidth(self.bgView)*0.5, 35*Scale);
            [self.okBtn setTitleColor:THEMECOLOR_OF_RED forState:UIControlStateNormal];
            [self.okBtn setTitle:@"同意" forState:UIControlStateNormal];
        } else if ([data.customModel.type isEqualToString:@"apply_voice"]) {
            self.bgView.hidden = NO;
            self.titleLbl.text = data.customModel.content;
            self.okBtn.frame = CGRectMake(EndX(self.noBtn), 65*Scale, ViewWidth(self.bgView)*0.5, 35*Scale);
            [self.okBtn setTitleColor:THEMECOLOR_OF_RED forState:UIControlStateNormal];
            [self.okBtn setTitle:@"同意" forState:UIControlStateNormal];
        } else if ([data.customModel.type isEqualToString:@"namecard"]) {
            self.cardview.hidden = NO;
            self.cardview.model = data.customModel.info;
        } else if ([data.customModel.type isEqualToString:@"resume_info"]) {
            self.personalView.hidden = NO;
            [self.personalView reloadDataModel:data.customModel.personalInfoModel];;
        } else if ([data.customModel.type isEqualToString:@"job_info"]) {
            self.jobView.hidden = NO;
            [self.jobView reloadDataModel:data.customModel.jobInfoModel];
        } else if ([data.customModel.type isEqualToString:@"resume"]) {
            self.bgView.hidden = NO;
            self.titleLbl.text = data.customModel.content;
            self.okBtn.frame = CGRectMake(0, 65*Scale, ViewWidth(self.bgView), 35*Scale);
            [self.okBtn setTitleColor:THEMECOLOR_OF_ORANGE forState:UIControlStateNormal];
            [self.okBtn setTitle:@"点击查看" forState:UIControlStateNormal];
        } else if ([data.customModel.type isEqualToString:@"location"]) {
            self.locationLbl.text = [NSString stringWithFormat:@"地址：%@", (data.customModel.desc&&data.customModel.desc.length)?data.customModel.desc:@""];
            self.locationBgView.hidden = NO;
        } else if ([data.customModel.type isEqualToString:@"notice"]) {
            self.noticeBgView.hidden = NO;
//            if (self.noticeBgView.viewHeight>1) {
//            } else {
//                [self.noticeBgView reloadDataForModel:data.customModel];
//            }
            [self.noticeBgView reloadDataForModel:data.customModel];
        }
    } else {
        _content.text = data.content;
        _content.textColor = THEMECOLOR_OF_TEXT_9a9a9a;
        _content.backgroundColor = COLOR_OF_CLEAR;
        
        //update layout
        CGSize textSize = [_content sizeThatFits:CGSizeMake(TSystemMessageCell_Text_Width_Max, MAXFLOAT)];
        CGFloat width = textSize.width + 2 * TSystemMessageCell_Margin;
        CGFloat height = textSize.height + 2 * TSystemMessageCell_Margin;
        _content.frame = CGRectMake((Screen_Width - width) * 0.5, TMessageCell_Padding, width, height);
    }
}
- (void)setupViews {
    self.backgroundColor = [UIColor clearColor];
    
    _content = [[UILabel alloc] init];
    _content.font = [UIFont systemFontOfSize:12];
    _content.textColor = [UIColor whiteColor];
    _content.textAlignment = NSTextAlignmentCenter;
    _content.numberOfLines = 0;
//    _content.backgroundColor = TSystemMessageCell_Background_Color;
    _content.layer.cornerRadius = 3;
    [_content.layer setMasksToBounds:YES];
    [self addSubview:_content];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.noticeBgView = [[ChatNoticeView alloc] initWithFrame:CGRectMake(15*Scale, TMessageCell_Padding, WIDTH-30*Scale, 90*Scale)];
    self.noticeBgView.hidden = YES;
    [self.noticeBgView addTarget:self action:@selector(noticeBgViewTap) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.noticeBgView];
    
    self.locationBgView = [UIToolsControl initViewWithFrame:CGRectMake(100*Scale, TMessageCell_Padding, WIDTH-200*Scale, 90*Scale) backgroundColor:COLOR_OF_WHITE];
    self.locationBgView.layer.cornerRadius = 5;
    self.locationBgView.layer.masksToBounds = YES;
    [self addSubview:self.locationBgView];
    self.locationBgView.hidden = YES;
    self.locationLbl = [UIToolsControl initLabelWithFrame:CGRectMake(0, 0, ViewWidth(self.locationBgView), 55*Scale) text:nil textColor:THEMECOLOR_OF_TEXT_666666 font:Font_13];
    self.locationLbl.textAlignment = NSTextAlignmentCenter;
    self.locationLbl.numberOfLines = 0;
    self.locationLbl.lineBreakMode = NSLineBreakByWordWrapping;
    [self.locationBgView addSubview:self.locationLbl];
    UILabel *locationBottomLbl = [UIToolsControl initLabelWithFrame:CGRectMake(0, EndY(self.locationLbl), ViewWidth(self.locationBgView), 35*Scale) text:@"点击查看" textColor:THEMECOLOR_OF_BLUE font:Font_13];
    locationBottomLbl.textAlignment = NSTextAlignmentCenter;
    [self.locationBgView addSubview:locationBottomLbl];
    [locationBottomLbl borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeTop];
    UIButton *locationBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, 0, ViewWidth(self.locationBgView), ViewHeight(self.locationBgView)) imageName:nil target:self action:@selector(locationBtnTap) title:nil titleColor:nil];
    [self.locationBgView addSubview:locationBtn];
    
    self.cardview = [[LYBusinessCardView alloc] initWithFrame:CGRectMake(20*Scale, TMessageCell_Padding, WIDTH-40*Scale, (WIDTH-40.0*Scale)*685/1005)];
    self.cardview.layer.cornerRadius = 5;
    self.cardview.layer.masksToBounds = YES;
    [self addSubview:self.cardview];
    self.cardview.hidden = YES;
    
    self.personalView = [[IMPersonalBaseInfoView alloc] initWithFrame:CGRectMake(15*Scale, TMessageCell_Padding, WIDTH-30*Scale, 100*Scale)];
    self.personalView.backgroundColor = COLOR_OF_WHITE;
    self.personalView.layer.cornerRadius = 5;
    self.personalView.layer.masksToBounds = YES;
    [self addSubview:self.personalView];
    self.personalView.hidden = YES;
    [self.personalView addTarget:self action:@selector(personalViewTap) forControlEvents:UIControlEventTouchUpInside];
    
    self.jobView = [[IMJobBaseInfoView alloc] initWithFrame:CGRectMake(15*Scale, TMessageCell_Padding, WIDTH-30*Scale, 100*Scale)];
    self.jobView.backgroundColor = COLOR_OF_WHITE;
    self.jobView.layer.cornerRadius = 5;
    self.jobView.layer.masksToBounds = YES;
    [self addSubview:self.jobView];
    self.jobView.hidden = YES;
    [self.jobView addTarget:self action:@selector(jobViewTap) forControlEvents:UIControlEventTouchUpInside];
    
    self.bgView = [UIToolsControl initViewWithFrame:CGRectMake(80*Scale, TMessageCell_Padding, (WIDTH-160*Scale), 100*Scale) backgroundColor:COLOR_OF_WHITE];
    self.bgView.layer.cornerRadius = ScalePS(8);
    self.bgView.layer.masksToBounds = YES;
    [self addSubview:self.bgView];
    self.bgView.hidden = YES;
    self.iconImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(20*Scale, 15*Scale, 15*Scale, 20*Scale) imageName:@"chat_fasongjianli"];
    [self.bgView addSubview:self.iconImgView];
    self.titleLbl = [UIToolsControl initLabelWithFrame:CGRectMake(55*Scale, 0, ViewWidth(self.bgView)-60*Scale, 65*Scale) text:nil textColor:THEMECOLOR_OF_TEXT_333333 font:12];
    self.titleLbl.numberOfLines = 0;
    self.titleLbl.lineBreakMode = NSLineBreakByWordWrapping;
    [self.bgView addSubview:self.titleLbl];
    UIView *lineView = [UIToolsControl initViewWithFrame:CGRectMake(0, 64.5*Scale, ViewWidth(self.bgView), lineH) backgroundColor:RGB(220, 220, 220)];
    [self.bgView addSubview:lineView];
    self.noBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, 65*Scale, ViewWidth(self.bgView)*0.5, 35*Scale) imageName:nil target:self action:@selector(noBtnTap) title:@"拒绝" titleColor:THEMECOLOR_OF_TEXT_9a9a9a];
    self.noBtn.titleLabel.font = Font(15);
    self.noBtn.backgroundColor = COLOR_OF_WHITE;
    [self.bgView addSubview:self.noBtn];
    self.okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(EndX(self.noBtn), 65*Scale, ViewWidth(self.bgView)*0.5, 35*Scale) imageName:nil target:self action:@selector(okBtnTap) title:@"同意" titleColor:THEMECOLOR_OF_RED];
    self.okBtn.titleLabel.font = Font(15);
    self.okBtn.backgroundColor = COLOR_OF_WHITE;
    [self.bgView addSubview:self.okBtn];
    
    [self.noBtn borderColor:RGB(220, 220, 220) borderWidth:lineH borderType:BorderSideTypeRight];
}
- (void)personalViewTap {
    if (customDataModel.customModel.personalInfoModel.id && customDataModel.customModel.personalInfoModel.id.length) {
        NSDictionary *dic = @{@"id":customDataModel.customModel.personalInfoModel.id};
        if (dic && [dic count]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:NotificationFor_IM_OK_LookPersonal object:nil userInfo:dic];
        }
    }
}
- (void)jobViewTap {
    if (customDataModel.customModel.jobInfoModel.id && customDataModel.customModel.jobInfoModel.id.length) {
        NSDictionary *dic = @{@"id":customDataModel.customModel.jobInfoModel.id};
        if (dic && [dic count]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:NotificationFor_IM_OK_LookCompanyJob object:nil userInfo:dic];
        }
    }
}
- (void)noticeBgViewTap {
    if (customDataModel.customModel.actions.count) {
        NSDictionary *dic = customDataModel.customModel.actions[0];
        if (dic && [dic count]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:NotificationFor_IM_OK_LookNotice object:nil userInfo:dic];
        }
    }
}
- (void)locationBtnTap {
    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationFor_IM_OK_LookAddressMap object:nil userInfo:@{@"desc":customDataModel.customModel.desc, @"latitude":[NSNumber numberWithDouble:customDataModel.customModel.latitude], @"longitude":[NSNumber numberWithDouble:customDataModel.customModel.longitude]}];
}
- (void)noBtnTap {
    NSLog(@"fdssdd");
    NSString *url = @"";
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    if ([customDataModel.customModel.type isEqualToString:@"namecard_exchange"]) {
        [dic setObject:customDataModel.customModel.rid forKey:@"id"];
        url = IM_OpNameCardExchange_URL;
        SVPHUDShow;
    } else if ([customDataModel.customModel.type isEqualToString:@"resume_apply"]) {
        [dic setObject:customDataModel.customModel.rid forKey:@"rid"];
        url = IM_OpApplyResume_URL;
        SVPHUDShow;
    } else if ([customDataModel.customModel.type isEqualToString:@"apply_video"]) {
        [dic setObject:customDataModel.customModel.id forKey:@"id"];
        url = IM_OpApplyVideo_URL;
    } else if ([customDataModel.customModel.type isEqualToString:@"apply_voice"]) {
        [dic setObject:customDataModel.customModel.id forKey:@"id"];
        url = IM_OpApplyVoice_URL;
    }
    [dic setObject:@"2" forKey:@"op"];
    [BaseNetManager requestWithMethod:POST path:url params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            SVPHUDSuccess(responseObject[@"msg"]);
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (void)okBtnTap {
    NSLog(@"okokokookokokokko");
    if ([customDataModel.customModel.type isEqualToString:@"resume"]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:NotificationFor_IM_OK_LookResume object:nil userInfo:@{@"resume_id":customDataModel.customModel.resume_id, @"rid":customDataModel.customModel.rid}];
    } else {
        NSString *url = @"";
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
        dic = [UIToolsControl getPublicParameter];
        if ([customDataModel.customModel.type isEqualToString:@"namecard_exchange"]) {
            [dic setObject:customDataModel.customModel.rid forKey:@"id"];
            url = IM_OpNameCardExchange_URL;
            SVPHUDShow;
        } else if ([customDataModel.customModel.type isEqualToString:@"resume_apply"]) {
            [dic setObject:customDataModel.customModel.rid forKey:@"rid"];
            url = IM_OpApplyResume_URL;
            SVPHUDShow;
        } else if ([customDataModel.customModel.type isEqualToString:@"apply_video"]) {
            [dic setObject:customDataModel.customModel.id forKey:@"id"];
            url = IM_OpApplyVideo_URL;
        } else if ([customDataModel.customModel.type isEqualToString:@"apply_voice"]) {
            [dic setObject:customDataModel.customModel.id forKey:@"id"];
            url = IM_OpApplyVoice_URL;
        }
        [dic setObject:@"1" forKey:@"op"];
        [BaseNetManager requestWithMethod:POST path:url params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
            if ([responseObject[@"code"] integerValue] == 2000) {
                SVPHUDDismiss;
                id data = responseObject[@"data"];
                if ([self->customDataModel.customModel.type isEqualToString:@"namecard_exchange"]) {
//                    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationFor_IM_OK_NamecardExchange object:nil];
                } else if ([self->customDataModel.customModel.type isEqualToString:@"resume_apply"]) {
                    
                } else if ([self->customDataModel.customModel.type isEqualToString:@"apply_video"]) {
    //                room_id
                    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationFor_IM_OK_ApplyVideo object:nil userInfo:@{@"roomID":data[@"room_id"], @"chatTime":data[@"chat_time"], @"id":self->customDataModel.customModel.id}];
                } else if ([self->customDataModel.customModel.type isEqualToString:@"apply_voice"]) {
                    //                room_id
                    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationFor_IM_OK_ApplyVoice object:nil userInfo:@{@"roomID":data[@"room_id"], @"chatTime":data[@"chat_time"], @"id":self->customDataModel.customModel.id}];
                }
            } else {
                SVPHUDError(responseObject[@"msg"]);
            }
        } failurBlock:^(NSError * _Nonnull error) {
            SVPHUDError(NetErrorAlertText);
        }];
    }
}

@end
